package com.company.dcm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.dcm.entity.R_DeviceInfo;
import com.company.dcm.mapper.R_DeviceInfoMapper;
import com.company.dcm.service.R_DeviceInfoService;
import com.company.dcm.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class R_DeviceInfoServiceImpl extends ServiceImpl<R_DeviceInfoMapper, R_DeviceInfo> implements R_DeviceInfoService {
    @Autowired
    private R_DeviceInfoMapper r_deviceInfoMapper;

    @Override
    public R_DeviceInfo getLastR_DeviceInfoByDeviceCode(String deviceCode) {
        return r_deviceInfoMapper.selectOne(new LambdaQueryWrapper<R_DeviceInfo>()
                        .eq(R_DeviceInfo::getDeviceCode,deviceCode)
                        .orderBy(true,false,R_DeviceInfo::getId)
                        .last("limit 1"));
    }

    @Override
    public Boolean updateOrInsertDeviceInfo(R_DeviceInfo deviceInfo){
        if(!StringUtils.isNullOrEmpty(deviceInfo.getDeviceCode())){
            if(r_deviceInfoMapper.update(deviceInfo,
                    new LambdaUpdateWrapper<R_DeviceInfo>().eq(R_DeviceInfo::getDeviceCode,deviceInfo.getDeviceCode()))<1){
                return r_deviceInfoMapper.insert(deviceInfo) >= 1;
            }
        }
        return true;
    }
}
